package starComponent.Listeners;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.SwingUtilities;

import starComponent.elements.FlyingNameLabel;
import tuio.SpecialTouchEvent;

/**
 * @author Charles Perin
 * 21 avr. 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 */
public class ListenerFlyingNameLabel implements MouseListener, MouseMotionListener{

	private FlyingNameLabel flyingNameLabel;

	public ListenerFlyingNameLabel(FlyingNameLabel flyingNameLabel) {
		this.setFlyingNameLabel(flyingNameLabel);
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		Point p = new Point(e.getX(), e.getY());
		p = SwingUtilities.convertPoint(flyingNameLabel, p, flyingNameLabel.getBranch().getStar());
		flyingNameLabel.getBranch().draggBranch(p);
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount()==2){
			flyingNameLabel.getBranch().openPopup(e.getX(), e.getY());
		}
		else{
			if(flyingNameLabel.getBranch().isPopupOpen()){
				Point p = SwingUtilities.convertPoint(flyingNameLabel, new Point(e.getX(), e.getY()), flyingNameLabel.getBranch().getPopupBranch());
				flyingNameLabel.getBranch().getPopupBranch().clickOn(p.x, p.y);
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		if(e instanceof SpecialTouchEvent){
			SpecialTouchEvent ste = (SpecialTouchEvent)e;
			switch(ste.getTouchModifier()){
			case SpecialTouchEvent.LONG_TOUCH:
				flyingNameLabel.getBranch().selectBranch();
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		if(flyingNameLabel.getBranch().isBranchSelected()) flyingNameLabel.getBranch().releaseBranch();
	}

	public void setFlyingNameLabel(FlyingNameLabel flyingNameLabel) {
		this.flyingNameLabel = flyingNameLabel;
	}

	public FlyingNameLabel getFlyingNameLabel() {
		return flyingNameLabel;
	}

}
